#!/bin/bash

# add_repo() Example Usage:
#
# ### Input Data
#
# Let’s assume the following input variables are set in the shell:
#
# ```sh
# mount_repo_name="OS everything"
# mount_repo_addr="http://repo.openeuler.org/openEuler-20.03-LTS/OS/$basearch/ http://repo.openeuler.org/openEuler-20.03-LTS/everything/$basearch/"
# ```
#
# - `mount_repo_name` contains two repository names: `OS` and `everything`.
# - `mount_repo_addr` contains the corresponding base URLs for these repositories.
#
# ---
#
# ### Function Execution
#
# When the `add_repo` function is called, it will:
#
# 1. Split `mount_repo_name` and `mount_repo_addr` into individual repository names and addresses.
# 2. Create `.repo` files in `/etc/yum.repos.d/` for each repository.
# 3. Append the repository configuration to the respective `.repo` files.
#
# ---
#
# ### Output Files
#
# #### 1. `/etc/yum.repos.d/OS.repo`
#
# ```ini
# [OS]
# name=OS
# baseurl=http://repo.openeuler.org/openEuler-20.03-LTS/OS/$basearch/
# enabled=$(check_yum_repo http://repo.openeuler.org/openEuler-20.03-LTS/OS/$basearch/)
# gpgcheck=0
# priority=100
#
# [OS-source]
# name=OS-source
# baseurl=http://repo.openeuler.org/openEuler-20.03-LTS/source/
# enabled=$(check_yum_repo http://repo.openeuler.org/openEuler-20.03-LTS/source/)
# gpgcheck=0
# priority=100
# skip_if_unavailable=True
# ```
#
# #### 2. `/etc/yum.repos.d/everything.repo`
#
# ```ini
# [everything]
# name=everything
# baseurl=http://repo.openeuler.org/openEuler-20.03-LTS/everything/$basearch/
# enabled=$(check_yum_repo http://repo.openeuler.org/openEuler-20.03-LTS/everything/$basearch/)
# gpgcheck=0
# priority=100
#
# [everything-source]
# name=everything-source
# baseurl=http://repo.openeuler.org/openEuler-20.03-LTS/source/
# enabled=$(check_yum_repo http://repo.openeuler.org/openEuler-20.03-LTS/source/)
# gpgcheck=0
# priority=100
# skip_if_unavailable=True
# ```

add_repo()
{
	# Check if both mount_repo_name and mount_repo_addr are non-empty
	[ -n "$mount_repo_name" ] && [ -n "$mount_repo_addr" ] || return 0

	# Use positional parameters to simulate arrays
	set -- "$mount_repo_name"
	set -- "$@" "$mount_repo_addr"

	# Split mount_repo_name and mount_repo_addr into lists
	IFS=' '
	set -- $mount_repo_name $mount_repo_addr

	# Count the number of repositories
	repo_count=$(echo "$mount_repo_name" | wc -w)

	# Iterate over the repositories
	i=1
	while [ "$i" -le "$repo_count" ]; do
		# Extract repo name and address
		repo_name=$(echo "$mount_repo_name" | cut -d' ' -f"$i")
		repo_addr=$(echo "$mount_repo_addr" | cut -d' ' -f"$i")

		# Replace '/' with '-' in the repo name to create a valid filename
		repo_file_name=$(echo "$repo_name" | tr '/' '-')

		# Append the repository configuration to the .repo file
		cat <<EOF >> "/etc/yum.repos.d/${repo_file_name}.repo"
[${repo_file_name}]
name=${repo_file_name}
baseurl=${repo_addr}
enabled=$(check_yum_repo "${repo_addr}")
gpgcheck=0
priority=100

[${repo_file_name}-source]
name=${repo_file_name}-source
baseurl=${repo_addr%/*}/source
enabled=$(check_yum_repo "${repo_addr%/*}/source")
gpgcheck=0
priority=100
skip_if_unavailable=True
EOF

		# Increment the counter
		i=$((i + 1))
	done
}

check_yum_repo()
{
	curl "${1%/*}" | grep -q '404 Not Found'
	if [ $? -eq 0 ]; then
		echo 0
	else
		echo 1
	fi
}

fix_on_distro()
{
	case "${compat_os}" in
		compat-centos8)
			# New version macro adapt
			sed -i '/%{?fedora} >\|%{?rhel} >/ s/$/ || %{?openEuler} == 2/' ${spec_dir}/*.spec
			;;
		compat-fedora33)
			# Add fedora33 macro define
			if $(grep vimfiles_root ${spec_dir}/*.spec >/dev/null); then
				sed -i '1 i%global vimfiles_root %{_datadir}/vim/vimfiles' ${spec_dir}/*.spec
			fi
			# Fix fedora33 uniq macro: %cmake_build %cmake_install
			# ref: https://docs.fedoraproject.org/en-US/packaging-guidelines/CMake/
			sed -i '/^%cmake3/ s;$; ../;' ${spec_dir}/*.spec
			sed -i '/^%cmake3/i mkdir build && cd build/' ${spec_dir}/*.spec
			sed -i '/^%cmake_build/a %__cmake --build . %{?_smp_mflags} --verbose' ${spec_dir}/*.spec
			sed -i '/^%cmake_install/a DESTDIR="%{buildroot}" %__cmake --install ./build' ${spec_dir}/*.spec
			sed -i '/%cmake_install\|%cmake_build/ s/^/#/' ${spec_dir}/*.spec
			;;
	esac
}

yum_repo_retry()
{
        for i in {1..3}
        do
                yum makecache > /dev/null && return
                [[ "${i}" < 3 ]] && sleep 10
        done
        exit 1
}

export_compat_os_base_version()
{
        . /etc/os-release

        case ${ID} in
                centos)
                        case ${VERSION_ID} in
                                6)
                                        export compat_os=compat-centos6
                                        ;;
                                7)
                                        export compat_os=compat-centos7
                                        ;;
                                8)
                                        export compat_os=compat-centos8
                                        ;;
                        esac
			;;
                fedora)
                        case ${VERSION_ID} in
                                33)
                                        export compat_os=compat-fedora33
                                        ;;
                                34)
                                        export compat_os=compat-fedora34
                                        ;;
                        esac
			;;
	esac
}
